package com.luosan.portalservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luosan.portalservice.pojo.MainCard;
import com.luosan.portalservice.service.MainCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/api/cards/main")
public class MainCardController {
    @Autowired
    private MainCardService cardService;

    // 分页查询
    @GetMapping("/page")
    public Page<MainCard> pageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<MainCard> result = cardService.getPageList(page, size);
        log.info(String.valueOf(result.getRecords()));
        return result;
    }

    // 新增主菜单
    @PostMapping
    public String create(@RequestBody MainCard card) {
        card.setSortOrder((int) (cardService.count() + 1));
        return cardService.save(card) ? "ok" : "创建失败";
    }

}
